package com.freight.msg.controller;

import com.freight.core.common.annotation.SystemLog;
import com.freight.core.common.enums.LogType;
import com.freight.core.common.utils.PageUtil;
import com.freight.core.common.utils.ResultUtil;
import com.freight.core.common.vo.PageVo;
import com.freight.core.common.vo.Result;
import com.freight.file.constants.FileConstant;
import com.freight.file.entity.File;
import com.freight.file.service.FileService;
import com.freight.msg.dto.MsgConsultingComplaintsDto;
import com.freight.msg.dto.MsgConsultingDto;
import com.freight.msg.dto.MsgConsultingRecordDto;
import com.freight.msg.entity.MsgConsultingComplaints;
import com.freight.msg.entity.MsgConsultingComplaintsRecord;
import com.freight.msg.service.MsgConsultingComplaintsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
@Api(description = "用户投诉接口")
@RequestMapping("/freight/msgComplaint")
@Transactional
public class MsgComplaintController {

    @Autowired
    FileService fileService;

    @Autowired
    private MsgConsultingComplaintsService msgConsultingComplaintsService;

    @RequestMapping(value = "/msgComplaintevaluate", method = RequestMethod.POST)
    @ApiOperation(value = "投诉评分")
    public Result<String> msgComplaintevaluate(Long msgComplaintId,Integer level) {
        msgConsultingComplaintsService.evaluate(msgComplaintId,level);
        return new ResultUtil<String>().setData("操作成功");
    }

    @RequestMapping(value = "/getmsgComplaintIdList", method = RequestMethod.POST)
    @ApiOperation(value = "获取投诉详细处理记录")
    public Result<List<MsgConsultingComplaintsRecord>> getmsgComplaintIdList(Long msgComplaintId) {
        List<MsgConsultingComplaintsRecord> page = msgConsultingComplaintsService.getmsgConsultingIdList(msgComplaintId);
        return new ResultUtil<List<MsgConsultingComplaintsRecord>>().setData(page);
    }

    @RequestMapping(value = "/getMsgComplaintList", method = RequestMethod.GET)
    @ApiOperation(value = "获取投诉列表")
    public Result<org.springframework.data.domain.Page<MsgConsultingComplaints>> getMsgComplaintList(MsgConsultingComplaintsDto msgConsultingComplaintsDto, Integer handleState, PageVo pageVo) {
        org.springframework.data.domain.Page<MsgConsultingComplaints> page = msgConsultingComplaintsService.getMsgConsultingList(msgConsultingComplaintsDto,handleState,PageUtil.initPage(pageVo),"2");
        return new ResultUtil<org.springframework.data.domain.Page<MsgConsultingComplaints>>().setData(page);
    }

    @RequestMapping(value = "/saveMsgComplaint", method = RequestMethod.POST)
    @ApiOperation(value = "保存投诉信息")
    public Result<String> saveMsgComplaint(Long orderId,MsgConsultingDto msgConsultingDto,@RequestParam(required = false) MultipartFile[] file) {
        String url="";
        if(file!=null&&file.length>0) {
            for (int i = 0; i < file.length; i++) {
                File sfile = fileService.saveFileByUserId(file[i], FileConstant.CONSULTING);
                url = url + sfile.getUrl() + "";
                if (i < file.length - 1) {
                    url = url + ",";
                }
            }
        }
        msgConsultingComplaintsService.saveMsgConsulting(orderId,msgConsultingDto,"2",url);
        return new ResultUtil<String>().setData("操作成功");
    }

    @RequestMapping(value = "/saveMsgComplaintRecord", method = RequestMethod.POST)
    @ApiOperation(value = "保存投诉处理记录")
    public Result<String> saveMsgComplaintRecord(Long msgComplaintId, MsgConsultingRecordDto msgConsultingRecordDto,@RequestParam(required = false) MultipartFile[] file) {
        String url="";
        if(file!=null&&file.length>0) {
            for (int i = 0; i < file.length; i++) {
                File sfile = fileService.saveFileByUserId(file[i], FileConstant.CONSULTING);
                url = url + sfile.getUrl() + "";
                if (i < file.length - 1) {
                    url = url + ",";
                }
            }
        }
        msgConsultingComplaintsService.saveMsgConsultingRecord(msgComplaintId,msgConsultingRecordDto,url);
        return new ResultUtil<String>().setData("操作成功");
    }

    @RequestMapping(value = "/endMsgComplaint", method = RequestMethod.POST)
    @ApiOperation(value = "结束投诉")
    public Result<String> endMsgComplaint(Long msgConsultingId) {
        msgConsultingComplaintsService.endMsgConsulting(msgConsultingId);
        return new ResultUtil<String>().setData("操作成功");
    }

    @RequestMapping(value = "/uploadComplaintPicId", method = RequestMethod.POST)
    @ApiOperation(value = "上传投诉图片")
    @SystemLog(description = "上传投诉图片", type = LogType.OPERATION)
    public Result<String> uploadComplaintPicId(@RequestParam(required = false) List<MultipartFile> file) {
        String url="";
        for(int i = 0; i < file.size(); i++){
            File sfile = fileService.saveFileByUserId(file.get(0), FileConstant.COMPLAINT);
            url = url + sfile.getId()+"";
            if(i< file.size()-1){
                url = url + ",";
            }
        }
        return ResultUtil.data(url);
    }

}
